﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace 表单数据绑定
{


    /// <summary>
    /// UserList.xaml 的交互逻辑
    /// </summary>
    public partial class UserList : Window
    {
        public UserList()
        {
            InitializeComponent();
            DataContext = new UserListViewModel();
        }
    }


    public class UserListViewModel
    {
        public ObservableCollection<User> Users { get; set; }
        private User _selectedUser;
        public User SelectedUser
        {
            get => _selectedUser;
            set { _selectedUser = value; OnPropertyChanged(); }
        }

        public ICommand AddCommand { get; }
        public ICommand EditCommand { get; }
        public ICommand DeleteCommand { get; }
        public ICommand ViewCommand { get; }

        public UserListViewModel()
        {
            Users = new ObservableCollection<User>
            {
                new User { Name = "张三", Age = "25", Email = "zhangsan@example.com" },
                new User { Name = "李四", Age = "30", Email = "lisi@example.com" },
                new User { Name = "王五", Age = "28", Email = "wangwu@example.com" }
            };

            AddCommand = new RelayCommand(_ => AddUser());
            EditCommand = new RelayCommand(_ => EditUser(), _ => SelectedUser != null);
            DeleteCommand = new RelayCommand(_ => DeleteUser(), _ => SelectedUser != null);
            ViewCommand = new RelayCommand(_ => ViewUser(), _ => SelectedUser != null);

        }

        private void AddUser()
        {
            var user = new User { Name = "新用户", Age = "0", Email = "new@example.com" };

            // 弹出添加用户表单
            var addUserForm = new AddUserForm { DataContext = new AddUserFormViewModel { User = user } };
            addUserForm.ShowDialog();

            Users.Add(user);
            SelectedUser = user;
        }

        private void EditUser()
        {
            if (SelectedUser != null)
            {
                // 这里只做简单演示，实际可弹窗编辑
                SelectedUser.Name += " (已编辑)";
            }
        }

        private void DeleteUser()
        {
            if (SelectedUser != null)
            {
                Users.Remove(SelectedUser);
            }
        }

        private void ViewUser()
        {
            if (SelectedUser != null)
            {
                MessageBox.Show($"姓名: {SelectedUser.Name}\n年龄: {SelectedUser.Age}\n邮箱: {SelectedUser.Email}", "查看用户");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
}
